/*********************************************************************
 * INCLUDES
 */
#include "OSAL.h"
#include "gpio.h"
#include "clock.h"
#include "timer.h"
#include "halPeripheral.h"
#include "log.h"
#include "pwm.h"

static uint8 pwm_TaskID; 
PwmCfg_t  pwm_config,pwm_config1,pwm_config2;

#define   PWN_PIN_CH0	P0
#define   PWN_PIN_CH1	P7
#define   PWN_PIN_CH2	P2
#define   PWN_PIN_CH3	P3
#define   PWN_PIN_CH4	P15
#define   PWN_PIN_CH5	P18
#define	  PWM_TOP_VALUE 256
#define	  PWM_COM_VALUE	128

/**
* @fn void sample_pwm_init()
* @brief Initialize pwm function
* @param NONE
* @return NONE.
*/
void sample_pwm_init()
{

	pwm_config.pwmN = PWM_CH0;
	pwm_config.pwmMode = PWM_CNT_UP;
	pwm_config.pwmPin = PWN_PIN_CH0;
	pwm_config.pwmDiv = PWM_CLK_DIV_64;
	pwm_config.pwmPolarity = PWM_POLARITY_RISING;
	pwm_config.cmpVal	= PWM_COM_VALUE;
	pwm_config.cntTopVal = PWM_TOP_VALUE;
	
	pwm_config1.pwmN = PWM_CH1;
	pwm_config1.pwmMode = PWM_CNT_UP;
	pwm_config1.pwmPin = PWN_PIN_CH1;
	pwm_config1.pwmDiv = PWM_CLK_DIV_16;
	pwm_config1.pwmPolarity = PWM_POLARITY_RISING;
	pwm_config1.cmpVal	= PWM_COM_VALUE;
	pwm_config1.cntTopVal = PWM_TOP_VALUE;
	
	pwm_config2.pwmN = PWM_CH2;
	pwm_config2.pwmMode = PWM_CNT_UP;
	pwm_config2.pwmPin = PWN_PIN_CH2;
	pwm_config2.pwmDiv = PWM_CLK_DIV_32;
	pwm_config2.pwmPolarity = PWM_POLARITY_RISING;
	pwm_config2.cmpVal	= PWM_COM_VALUE;
	pwm_config2.cntTopVal = PWM_TOP_VALUE;

	HalPwmModuleInit();
	HalPwmChannelStart(pwm_config);

}

/**
* @fn void sample_pwm_start()
* @brief pwm sample function
* @param NONE
* @return NONE.
*/
void sample_pwm_start()
{
	static uint16_t step = 0;
	switch(step){
		case 0:{	
			pwm_config.cmpVal += 5;
			HalPwmChannelStart(pwm_config);
			if(pwm_config.cmpVal >= PWM_TOP_VALUE){
				pwm_config.cmpVal = 5;
				HalPwmChannelStop(pwm_config.pwmN);
				step ++;
			}

		}break;
		case 1:{
			pwm_config1.cmpVal += 10;
			HalPwmChannelStart(pwm_config1);
			if(pwm_config1.cmpVal >= PWM_TOP_VALUE){
				pwm_config1.cmpVal = 10;
				HalPwmChannelStop(pwm_config1.pwmN);
				step ++;
			}
		}break;
		case 2:{
			pwm_config2.cmpVal += 10;
			HalPwmChannelStart(pwm_config2);
			if(pwm_config2.cmpVal >= PWM_TOP_VALUE){
				pwm_config2.cmpVal = 10;
				HalPwmChannelStop(pwm_config2.pwmN);
				step = 0;
			}
		}break;
		case 3:{

		}break;
		case 4:{

		}break;
		case 5:{

		}break;
	}
	
	
//	LOG("cmp_value:%d\r\n",pwm_config.cmpVal);
//	LOG("cmp_value1:%d\r\n",pwm_config1.cmpVal);
	
}
 
/*********************************************************************
 * @fn      AP_TIMER_Demo_Init
 *
 * @brief   
 *
 * @param   
 *
 * @return  
 ****************************************************************/
void HalPeripheral_Init( uint8 task_id )
{
	pwm_TaskID = task_id;

	LOG("init pwm\n");
	sample_pwm_init();
	VOID osal_start_timerEx( pwm_TaskID, PWM_TEST_EVT, 520);
}
 
/*********************************************************************
 * @fn      HalPeripheral_ProcessEvent
 *
 * @brief   Peripheral Test Task event processor.  This function
 *          is called to process PWM_TEST_EVT events for the task. 
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process PWM sample function.
 *
 * @return  events not processed
 */

uint16 HalPeripheral_ProcessEvent( uint8 task_id, uint16 events )
{	
//	static uint8_t min_count = 5;
	
	if (events & PWM_TEST_EVT )
	{
		sample_pwm_start(); 
    VOID osal_start_timerEx( pwm_TaskID, PWM_TEST_EVT, 50);
		return ( events ^ PWM_TEST_EVT );		
	}  

	return 0;
}
